package com.kaibes.module.email.sender;

import java.util.Map;

import org.simplejavamail.api.email.Email;
import org.simplejavamail.email.EmailBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.context.Context;

import com.kaibes.web.thymeleaf.ThymeleafService;

@Service
public class MailService {

    @Autowired
    private MailConfig maiConfig;
    @Autowired
    private MailTemplateService mailTemplateService;
    @Autowired
    private ThymeleafService thymeleafService;
    
    public boolean sendSimpleMessge(String to, String subject, String content) {
        try {
            Email email = EmailBuilder.startingBlank().from(maiConfig.getUsername()).to(to).withSubject(subject).withPlainText(content).buildEmail();
            maiConfig.createMailer().sendMail(email, true);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    public boolean sendHtmlMessge(String to, String subject, String content) {
        try {
            Email email = EmailBuilder.startingBlank().from(maiConfig.getUsername()).to(to).withSubject(subject).withHTMLText(content).buildEmail();
            maiConfig.createMailer().sendMail(email, true);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    public boolean sendTemplateMessage(String to, String key, Map<String, Object> dataModel) {
        MailTemplate template = mailTemplateService.getByKey(key);
        if (template == null) {
            return false;
        }
        return sendTemplateMessage(to, template, dataModel);
    }
    
    public boolean sendTemplateMessage(String to, MailTemplate template, Map<String, Object> dataModel) {
        thymeleafService.getModel(dataModel);
        dataModel.put("email", to);
        
        Context context = new Context();
        context.setVariables(dataModel);
        
        String subject = thymeleafService.process(template.getTitle(), context);
        String content = thymeleafService.process(template.getHtml(), context);
        
        Email email = EmailBuilder.startingBlank().from(maiConfig.getUsername()).to(to).withSubject(subject).withHTMLText(content).buildEmail();
        maiConfig.createMailer().sendMail(email, true);
        return true;
    }
    
    
//    /**
//     * 发送带附件的邮件
//     *
//     * @param to       收件人
//     * @param subject  主题
//     * @param content  内容
//     * @param filePath 附件路径
//     */
//    public void sendMimeMessge(String to, String subject, String content, String filePath) {
//        MimeMessage message = mailSender.createMimeMessage();
//        try {
//            // true表示需要创建一个multipart message
//            MimeMessageHelper helper = new MimeMessageHelper(message, true);
//            helper.setFrom(mailConfig.getSENDER());
//            helper.setTo(to);
//            helper.setSubject(subject);
//            helper.setText(content, true);
//
//            FileSystemResource file = new FileSystemResource(new File(filePath));
//            String fileName = file.getFilename();
//            helper.addAttachment(fileName, file);
//
//            mailSender.send(message);
//        } catch (MessagingException e) {
////            logger.error("发送带附件的MimeMessge时发生异常！", e);
//        }
//    }
//
//    /**
//     * 发送带静态文件的邮件
//     *
//     * @param to       收件人
//     * @param subject  主题
//     * @param content  内容
//     * @param rscIdMap 需要替换的静态文件
//     */
//    public void sendMimeMessge(String to, String subject, String content, Map<String, String> rscIdMap) {
//        MimeMessage message = mailSender.createMimeMessage();
//        try {
//            // true表示需要创建一个multipart message
//            MimeMessageHelper helper = new MimeMessageHelper(message, true);
//            helper.setFrom(mailConfig.getSENDER());
//            helper.setTo(to);
//            helper.setSubject(subject);
//            helper.setText(content, true);
//
//            for (Map.Entry<String, String> entry : rscIdMap.entrySet()) {
//                FileSystemResource file = new FileSystemResource(new File(entry.getValue()));
//                helper.addInline(entry.getKey(), file);
//            }
//            mailSender.send(message);
//        } catch (MessagingException e) {
////            logger.error("发送带静态文件的MimeMessge时发生异常！", e);
//        }
//    }
}
